package cn.psh.controller;


import cn.psh.AjaxResult;
import cn.psh.domain.Product;
import cn.psh.domain.Store;
import cn.psh.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;
import cn.psh.service.StoreService;


@RestController
public class StoreController {

    @Autowired
    private StoreService storeService;
    @Autowired
    private ProductService productService;

    @PostMapping("/store")
    public AjaxResult getById(@RequestBody Product product) {

        try {
            Long  id = product.getId();
            Store store = storeService.queryById(id);
            Product product1 = productService.loadByName(product.getName());
            if(store!=null){
    //            store不为空说明还有库存直接在库存
                long num = store.getNumber()+product.getNumber();
                store.setNumber((int)num);
                storeService.update(store);
            }else {
//                否则就是没有直接创建添加
                Store sto = new Store();
                sto.setProduct_id(product1.getId());
                sto.setNumber(product.getNumber());
                storeService.save(sto);
            }
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.OJBK().setSuccess(false).setMessage("操作失败！");
        }

    }
}
